package Model;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Information.HireInfo;
import Information.News;

public class HireInfoDAO{
	
	private String EName;
	private String EType;
	private String EEmail;
	private int ETel;
	private String EAddress;
	private int EPostCode;
	private String Job;
	private int HireTotal;
	private String City;
	private String Describe;
	private Date PublishTime;
	private int TotalClick;
	
	public HireInfoDAO()
	{
		this.EName="";
		this.EType="";
		this.EEmail="";
		this.ETel=0;
		this.EAddress="";
		this.EPostCode=0;
		this.Job="";
		this.HireTotal=0;
		this.City="";
		this.Describe="";
		this.PublishTime=new Date(0);
		this.TotalClick=0;
	}
	
	private Date Date(int i) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean AddNewHireInfo(String EName,String EType,String EEmail,int ETel,String EAddress,int EPostCode,String Job,int HireTotal,String City,String Describe) throws SQLException, ClassNotFoundException
	{
		String sql ="insert into HireInfo (EName,EType,EEmail,ETel,EAddress,EPostCode,Job,HireTotal,City,Describe) values('"+EName+"','"+EType+"','"+EEmail+"','"+ETel+"','"+EAddress+"','"+EPostCode+"','"+Job+"','"+HireTotal+"','"+City+"','"+Describe+"')";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public ArrayList<HireInfo> SeeHireInfoByHNO(int HNO) throws SQLException, ClassNotFoundException
	{
		ArrayList<HireInfo> arrayList = null;
		HireInfo HireInfo = new HireInfo();
		String sql ="select * from HireInfo where HNO ='"+HNO+"'";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		Job = result.getString("Job");
		EName= result.getString("EName");
		EType= result.getString("EType");
		EEmail= result.getString("EEmail");
		ETel= result.getInt("ETel");
		EAddress= result.getString("EAddress");
		EPostCode = result.getInt("EPostCode");
		Job= result.getString("Job");
		HireTotal= result.getInt("HireTotal");
		City= result.getString("City");
		Describe= result.getString("Describe");
		PublishTime= result.getDate("PublishTime");
		TotalClick= result.getInt("TotalClick");
		HireInfo.SetHireInfoInfo(EName,EType,EEmail, ETel,EAddress, EPostCode,Job,HireTotal,City,Describe,PublishTime,TotalClick);
		arrayList.add(HireInfo);
		return arrayList;
	}
	public ArrayList<HireInfo> SeeAllHireInfo() throws SQLException, ClassNotFoundException
	{
		ArrayList<HireInfo> arrayList = new ArrayList<HireInfo>();
		
		String sql ="select * from HireInfo";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		while(result.next())
		{
			HireInfo hireInfo = new HireInfo();
			Job = result.getString("Job");
			EName= result.getString("EName");
			EType= result.getString("EType");
			EEmail= result.getString("EEmail");
			ETel= result.getInt("ETel");
			EAddress= result.getString("EAddress");
			EPostCode = result.getInt("EPostCode");
			Job= result.getString("Job");
			HireTotal= result.getInt("HireTotal");
			City= result.getString("City");
			Describe= result.getString("Describe");
			PublishTime= result.getDate("PublishTime");
			TotalClick= result.getInt("TotalClick");
			hireInfo.SetHireInfoInfo(EName,EType,EEmail, ETel,EAddress, EPostCode,Job,HireTotal,City,Describe,PublishTime,TotalClick);
			
			arrayList.add(hireInfo);
		}
		return arrayList;
	}
	public boolean DeleteHireInfoByHNO(int HNO) throws ClassNotFoundException, SQLException
	{
		String sql ="delete from HireInfo where HNO='"+HNO+"'";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	
	public boolean UpdateHireInfo(int HNO,String EName,String EType,String EEmail,int ETel,String EAddress,int EPostCode,String Job,int HireTotal,String City,String Describe,Date PublishTime,int TotalClick) throws ClassNotFoundException, SQLException
	{
		//can not update EUser
		String sql = "update HireInfo set EName='"+EName
		+"',set EType='"+EType
		+"',set EEmail='"+EEmail
		+"',set ETel='"+ETel
		+"',set EAddress='"+EAddress
		+"',set EPostCode='"+EPostCode
		+"',set Job='"+Job
		+"',set HireTotal='"+HireTotal
		+"',set City='"+City
		+"',set Describe='"+Describe
		+"',set PublishTime='"+PublishTime
		+"',set TotalClick='"+TotalClick+"'where HNO='"+HNO+"')";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;//update successfully
		}
		else
		{
			return false;//update failed
		}
	}
}